<?php
class Rcp_LpImport_EmployeeImport_Positions {
	
	/**
	 * Tablica obiektów RcpPositions gdzie klucz to ExtId (Id Stanowiska z bazy LP)
	 * @var RcpPositions[]
	 */
	private $_map;
	
	public function __construct() {
		$this->_map = RcpPositionsQuery::create()->find()->getArrayCopy('ExtId');
	}
	
	/**
	 * @param Lp LpPositions
	 * @return RcpPositions
	 */
	private function _add(LpPositions $lpPosition) {
		$extId = $lpPosition->getId();
		$position = new RcpPositions();
		$position->setExtId($extId);
		$this->_map[$extId] = $position;
		return $position;
	}
	
	/**
	 * @param LpPositions $lpPosition
	 * @return RcpPositions
	 */
	private function _getWithAdd(LpPositions $lpPosition) {
		$position = $this->get($lpPosition->getId());
		if (!($position instanceof RcpPositions))
			$position = $this->_add($lpPosition);
		return $position;
	}
	
	/**
	 * @param string $extId
	 * @return RcpPositions|NULL
	 */
	public function get($extId) {
		if (key_exists($extId, $this->_map))
			return $this->_map[$extId];
		return null;
	}
	
	/**
	 * @param LpPositions $lpPosition
	 * @return RcpPositions
	 */
	public function update(LpPositions $lpPosition) {
		$position = $this->_getWithAdd($lpPosition);
		$position->setNamePl($lpPosition->getNamePl());
		$position->setNameEn($lpPosition->getNameEn());
		//$position->setGrade($lpPosition->getGrade());
		return $position;
	}
	
}